﻿using System.Collections.Generic;
using System.Security.Cryptography;
using System.Linq;
using System.Web.Routing;
using System.Web.Mvc;
//using IndyMosaic.Models.Blog;
using System;
//using IndyMosaic.Models.Web;
using System.Linq.Expressions;
using System.Web.UI;
using System.IO;

namespace IndyMosaic.Core.Extensions
{
    public static class HtmlHelperExtensions
    {
        #region Gravatar
        public static string Gravatar(this HtmlHelper html, string email)
        {
            return GetImageTag(GetGravatar(email));
        }

        public static string Gravatar(this HtmlHelper html, string email, object gravatarAttributes)
        {
            return GetImageTag(GetGravatar(email, gravatarAttributes));
        }

        public static string Gravatar(this HtmlHelper html, string email, object gravatarAttributes, object htmlAttributes)
        {
            return GetImageTag(GetGravatar(email, gravatarAttributes), htmlAttributes);
        }

        private static string GetImageTag(string source)
        {
            return GetImageTag(source, null);
        }

        private static string GetImageTag(string source, object htmlAttributes)
        {

            IDictionary<string, object> attributes =
                (htmlAttributes == null
                    ? new RouteValueDictionary()
                    : new RouteValueDictionary(htmlAttributes));

            string returnVal = "<img src=\"{0}\"";

            foreach (string key in attributes.Keys)
            {
                returnVal += string.Format("{0}=\"{1}\"", key, attributes[key]);
            }
            returnVal += " />";
            return string.Format(returnVal, source);
        }

        private static string GetGravatar(string email)
        {
            //force the "G" rating
            return string.Format("http://www.gravatar.com/avatar/{0}?r=g", EncryptMD5(email));
        }

        private static string GetGravatar(string email, object gravatarAttributes)
        {
            IDictionary<string, object> attributes = (gravatarAttributes == null
                    ? new RouteValueDictionary()
                    : new RouteValueDictionary(gravatarAttributes));

            string returnVal = GetGravatar(email);
            bool first = true;

            foreach (string key in attributes.Keys)
            {
                if (first && !returnVal.Contains("?"))
                {
                    first = false;
                    returnVal += string.Format("?{0}={1}", key, attributes[key].ToString());
                    break;
                }
                returnVal += string.Format("&{0}={1}", key, attributes[key].ToString());
            }

            return returnVal;
        }

        private static string EncryptMD5(string Value)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] valueArray = System.Text.Encoding.ASCII.GetBytes(Value);
            valueArray = md5.ComputeHash(valueArray);
            string encrypted = "";
            for (int i = 0; i < valueArray.Length; i++)
                encrypted += valueArray[i].ToString("x2").ToLower();
            return encrypted;
        }
        #endregion

        #region Navigation
        public static string GetAnchorTag(this HtmlHelper html, object id)
        {
            return string.Format("<div id=\"{0}\" />", id.ToString());
        }
        #endregion

        #region Controls

        //public static SelectList ToSelectList(this HtmlHelper html, Enum enumObj)
        //{
        //    var values = from Enum e in Enum.GetValues(enumObj.GetType())
        //                 select new { Id = e, Name = e.GetDescription() };
            
        //    return new SelectList(values, "Id", "Name", enumObj);
        //}

        public static SelectList ToSelectList<TEnum>(this HtmlHelper html, TEnum enumObj)
        {
            var values = from TEnum e in Enum.GetValues(typeof(TEnum))
                         select new { Id = e, Name = e.GetDescription() };

            return new SelectList(values, "Id", "Name", enumObj);
        }
        #endregion


        #region ReCaptcha
        public static string GenerateCaptcha(this HtmlHelper helper)
        {
            var captchaControl = new Recaptcha.RecaptchaControl
            {
                ID = "recaptcha",
                Theme = "blackglass",
                PublicKey = "6LfUEc4SAAAAAJ1XGgxoHpDjtXwyFoVz4vlQ2ZbM",
                PrivateKey = "6LfUEc4SAAAAAGfG_FFBjlODeiwJLAIrdSsTseQP"
            };

            var htmlWriter = new HtmlTextWriter(new StringWriter());
            captchaControl.RenderControl(htmlWriter);

            return htmlWriter.InnerWriter.ToString();
        }
        #endregion
    }
}
